package com.hxkj.service.taglib.impl;

import com.hxkj.client.service.WebsiteAddrServiceClient;
import com.hxkj.domain.po.WebsiteAddr;
import com.hxkj.service.taglib.WebsiteAddrService;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import com.platform.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * created by chenfangzhou on 2018/8/9
 */
@Service
@Slf4j
public class WebsiteAddrServiceImpl implements WebsiteAddrService {
    @Autowired
    private WebsiteAddrServiceClient websiteAddrServiceClient;

    @Override
    public ResultPageStruct<WebsiteAddr> getPages(WebsiteAddr websiteAddr, PageTo pageParam) throws Exception {
        try {
            ParameterDataStruct<WebsiteAddr> parameterDataStruct = new ParameterDataStruct<>();
            parameterDataStruct.setData(websiteAddr);
            if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
                pageParam.setPageSize(10);
                pageParam.setPageNum(1);
            }
            parameterDataStruct.setPageTo(pageParam);
            return websiteAddrServiceClient.getPages(parameterDataStruct);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public List<WebsiteAddr> getList(WebsiteAddr websiteAddr) throws Exception {
        try {
            return websiteAddrServiceClient.getList(websiteAddr);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultDataStruct<WebsiteAddr> get(WebsiteAddr websiteAddr) throws Exception {
        try {
            if (websiteAddr.getAddrId() == null) throw new CustomMessageException(401,"id为空!");
            ResultDataStruct result = websiteAddrServiceClient.get(websiteAddr);
            if (result.getCode() != 200) throw new CustomMessageException(403,"查询失败!");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultMessageStruct save(WebsiteAddr websiteAddr) throws Exception {
        try {
            if (websiteAddr.getSiteId() == null) throw new CustomMessageException(402,"站点id为空!");
            ResultMessageStruct result = websiteAddrServiceClient.saveWebsite(websiteAddr);
            if (result.getCode() != 200) throw new CustomMessageException(403,result.getMessage());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultMessageStruct update(WebsiteAddr websiteAddr) throws Exception {
        try {
            if (websiteAddr.getAddrId() == null) throw new CustomMessageException(401,"id为空!");
            ResultMessageStruct result = websiteAddrServiceClient.modifyWebsite(websiteAddr);
            if (result.getCode() != 200) throw new CustomMessageException(403,"修改失败!");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultMessageStruct delete(WebsiteAddr websiteAddr) throws Exception {
        try {
            if (StringUtils.isBlank(websiteAddr.getAddrIds())) throw new CustomMessageException(401,"id字符串为空!");
            ResultMessageStruct result = websiteAddrServiceClient.removeWebsite(websiteAddr);
            if (result.getCode() != 200) throw new CustomMessageException(402,"删除失败!");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
